package com.gitee.cui.redis.jedis;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Redis中ZSet类型测试
 * @author cuiqingqiang
 * @version 1.0
 * @date 2021/11/7 20:14
 **/
@Slf4j
public class ZSetTest {

    private static final String KEY = "salary";

    @Test
    public void zSetTest() {
        Jedis jedis = new Jedis("localhost", 6379);
        jedis.del(KEY);
        log.info("jedis.ping(): " + jedis.ping());
        // 向集合中添加元素
        jedis.zadd(KEY, 1000, "zhangsan");
        // 向集合中批量添加元素
        Map<String, Double> salaryMap = new HashMap<>();
        salaryMap.put("user1", 1000D);
        salaryMap.put("user2", 2000D);
        salaryMap.put("user3", 3000D);
        salaryMap.put("user4", 4000D);
        salaryMap.put("user5", 5000D);
        jedis.zadd(KEY, salaryMap);

        // 获取类型:zset
        log.info("jedis.type(KEY): " + jedis.type(KEY));
        // 获取集合元素的个数
        log.info("jedis.zcard(KEY): " + jedis.zcard(KEY));
        // 按照下标[起，止]获取元素
        log.info("jedis.zrange(KEY, 0,2): " + jedis.zrange(KEY, 0,2));
        // 按照下标[起，止]倒序遍历元素
        log.info("jedis.zrevrange(KEY, 0,2): " + jedis.zrevrange(KEY, 0,2));
        // 按照分数(薪资)[起，止]遍历元素
        log.info("jedis.zrangeByScore(KEY, 1000, 4500): " + jedis.zrangeByScore(KEY, 1000, 4500));
        // 按照分数范围获取数据，带回带分数数据
        Set<Tuple> tuples = jedis.zrangeByScoreWithScores(KEY, 1000, 10000);
        for (Tuple tuple : tuples) {
            log.info("Tuple: " + tuple.getElement() + "->" + tuple.getScore());
        }
        // 按照分数（薪资）[起，止]倒序遍历元素
        log.info("jedis.zrevrangeByScore(KEY, 1000, 3500): " + jedis.zrevrangeByScore(KEY, 3500, 1000));
        // 按照分数（薪资）[起，止]获取元素个数
        log.info("jedis.zcount(KEY, 1500, 4500): " + jedis.zcount(KEY, 1500, 4500));
        // 获取元素分值（薪资）
        log.info("jedis.zscore(KEY, \"user2\"): " + jedis.zscore(KEY, "user2"));
        // 获取元素的下标
        log.info("jedis.zrank(KEY, \"user1\"): " + jedis.zrank(KEY, "user1"));
        // 倒序获取元素的下标
        log.info("jedis.zrevrank(KEY, \"user1\"): " + jedis.zrevrank(KEY, "user1"));
        // 删除元素
        log.info("jedis.zrem(KEY, \"zhangsan\"): " + jedis.zrem(KEY, "zhangsan"));
        // 通过下标范围删除元素
        log.info("jedis.zremrangeByRank(KEY, 0, 1): " + jedis.zremrangeByRank(KEY, 0, 1));
        // 通过分数范围删除元素
        log.info("jedis.zremrangeByScore(KEY, 2500, 4500): " + jedis.zremrangeByScore(KEY, 2500, 4500));
        // 按照下标[起，止]遍历元素
        log.info("jedis.zrange(KEY, 0, -1): " + jedis.zrange(KEY, 0, -1));

        // 批量添加元素
        Map<String, Double> salary = new HashMap<>();
        salary.put("us1", 1500D);
        salary.put("us2", 2500D);
        salary.put("us3", 3500D);
        salary.put("us4", 4500D);
        jedis.zadd(KEY, salary);

        // 指定增加分数
        log.info("jedis.zincrby(KEY, 1000, \"us2\"): " + jedis.zincrby(KEY, 1000, "us2"));
        // 批量获取全部数据
        log.info("jedis.zrange(KEY, 0, -1): " + jedis.zrange(KEY, 0, -1));

        jedis.close();
    }
}
